﻿using Furion.DependencyInjection;
using Furion.DynamicApiController;
using HYHY_Core.Enums;
using HYHY_Core.Tool.Tool;
using HYQY_App.Admin_System.Interface;
using HYQY_App.Applet.Interface;
using HYQY_Core.Entities.Advertising;
using HYQY_Core.Entities.Ware;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static HYQY_App.Admin_System.Dtos.CommodityAdminDto;
using static HYQY_App.Applet.Dtos.HomeDto;

namespace HYQY_App.Applet.Service
{
    /// <summary>
    /// 小程序Home首页
    /// </summary>
    [ApiDescriptionSettings(ApiGroupEnumsCode.AppletHome, Name = "AppletHome")]
    public class HomeService : IDynamicApiController, ITransient, IHomeService
    {
        /// <summary>
        /// sql
        /// </summary>
        private readonly ISqlSugarClient _db;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="db"></param>
        public HomeService(ISqlSugarClient db)
        {
            _db = db;
        }

        /// <summary>
        /// 小程序首页广告轮播图
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        public async Task<SelectBannerListResp> SelectBannerList()
        {
            var nowTime = DateTime.Now;

            var list = await _db.Queryable<Advertising>()
               .Where(it => it.Type == (int)AdvertisingEnumsCode.Slideshow && it.IsEnable)
               .Where(it => it.BeginTime <= nowTime && it.EndTime > nowTime)
               .Select(t1 => new SelectBannerListOutput
               {
                   Type = EnumCommon.GetDescription((AdvertisingEnumsCode)1),
                   Aid = t1.Aid,
                   Path = t1.Path,
               })
                .ToListAsync();

            return new SelectBannerListResp { List = list, StatusCode = 200, Message = "调用成功" };
        }

        /// <summary>
        /// 首页-商品列表
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        public async Task<SelectGoodsListResp> SelectGoodsList(SelectGoodsListInput input)
        {
            RefAsync<int> total = 0;

            var list = await _db.Queryable<WareBank>()
                .Select(t1 => new SelectGoodsListOutput
                {
                    WareDetail = t1.WareDetail,
                    WareName = t1.WareName,
                    CategoryId = t1.CategoryId,
                    CreateMan = t1.CreateMan,
                    CreateTime = t1.CreateTime,
                    IsClassify = t1.IsClassify,
                    Price = t1.Price,
                    WareCode = t1.WareCode,
                    IsGuessWhatYouLike = t1.IsGuessWhatYouLike,
                })
                .MergeTable()
                .OrderBy(t1 => SqlFunc.GetRandom())
                .ToPageListAsync(input.Page, input.PageSize, total);
            var imges = await _db.Queryable<Images>()
               .Where(it => list.Select(t => t.WareCode).Contains(it.Code)).ToListAsync();

            var categorys = await _db.Queryable<Category>()
                .Where(it => list.Select(t => t.CategoryId).Contains(it.Id)).ToListAsync();

            foreach (var item in list)
            {
                item.Path = imges.FirstOrDefault(it => it.Code == item.WareCode)?.Url ?? string.Empty;
                item.CategoryName = categorys
                    .FirstOrDefault(it => it.Id == item.CategoryId)?.Name ?? string.Empty;
            }

            return new SelectGoodsListResp
            {
                Total = total,
                List = list,
                StatusCode = 200,
                Message = "调用成功"
            };
        }
    }
}
